package com.adjy.pathbuilders.world;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;

import com.adjy.pathbuilders.database.GameDatabase;
import com.adjy.pathbuilders.objects.GameEntityType;
import com.adjy.pathbuilders.rendering.loading.ImageFileParser;
import com.adjy.pathbuilders.scenario.Scenario;
import com.adjy.pathbuilders.scenario.internal.DefaultScenarioLoader;
import com.adjy.pathbuilders.scenario.internal.ScenarioDescription;

/**
 * LevelManager is a Manager for the possible Levels for the Game.
 */
public class LevelManager {
	
	private static final String SCENARIO_FILE = "scenarios_file";
	private static final String SELECTABLE_OBJECTS_FILE = "selectable_objects_file";
	
	private Context context;
	private HashMap<LevelCategory, List<Level>> allLevels;

	/**
	 * Creates the LevelManager
	 * 
	 * @param context
	 *            Context to be used when loading Scenarios
	 */
	public LevelManager(Context context) {
		this.context = context;
		allLevels = new HashMap<LevelCategory, List<Level>>();

		LevelCategory[] categories = LevelCategory.values();
		for (int i = 0; i < categories.length; i++) {
			allLevels.put(categories[i], new ArrayList<Level>());
		}

	}

	/**
	 * Get all loaded levels for the given category
	 * 
	 * @param category
	 *            Required Category for the Levels
	 * @return List<Level> - List of Levels
	 */
	public List<Level> getLevels(LevelCategory category) {
		return allLevels.get(category);
	}

	/**
	 * Load all Levels from files
	 * 
	 * @throws IOException
	 *             Throws Exception if any error occurs when opening or reading
	 *             files
	 */
	public void loadLevels() throws IOException {
		Level previous = null;
		DefaultScenarioLoader scenarioLoader = new DefaultScenarioLoader();
		int levelNumber = 1;
		
		List<ScenarioDescription> descriptions = scenarioLoader.getScenarioDescriptions(
				context.getAssets().open(SCENARIO_FILE));
		HashMap<Integer, List<GameEntityType>> selectableObjects = 
							ImageFileParser.parseSelectableObjectsFile(context.getAssets().open(SELECTABLE_OBJECTS_FILE)); 
		
		for (ScenarioDescription scenarioDescription : descriptions) {
			
			Scenario s = scenarioLoader.load( // Load Scenario
					context.getAssets().open(scenarioDescription.getScenarioPath()));
			if(!s.isEmpty()) {
				List<GameEntityType> objects = selectableObjects.get(levelNumber);
				levelNumber++;
				// Create Initial Level
				Level l = new Level(scenarioDescription.getCategory(), scenarioDescription.getScenarioID(),
						s, 0, 0, objects, previous, null);
				
				if(previous != null){
					previous.setNext(l);
				}
				previous = l;
				
				GameDatabase.getInstance().updateLevelFromBD(l); // coins, time
				allLevels.get(scenarioDescription.getCategory()).add(l);
			}
		}
	}

	/**
	 * Gets all loaded levels
	 * 
	 * @return List<Level> - All loaded levels
	 */
	public List<Level> getAllLevels() {
		List<Level> allLevels = new ArrayList<Level>();
		allLevels.addAll(this.allLevels.get(LevelCategory.EASY));
		allLevels.addAll(this.allLevels.get(LevelCategory.MEDIUM));
		allLevels.addAll(this.allLevels.get(LevelCategory.HARD));
		return allLevels;
	}

}
